#include "stdafx.h"
#include "ckCheckCaseManager.h"
#include "ckCheckCase.h"
#include "ckEnumCheckCase.h"
#include "ckEnumOperator.h"

#include "ckConnection.h"
#include "ckCommand.h"

#include "zModule.h"
#include "ckModuleManager.h"

namespace ck
{
	CheckCase* CreateCaseObject(ADODB::_RecordsetPtr pRecordset)
	{
		z_uint32 nID = (z_uint32)pRecordset->GetCollect("ID");
		//z_uint32 nCode = (z_uint32)pRecordset->GetCollect("CK_CODE");
		z_uint32 nCode = nID;// (z_uint32)pRecordset->GetCollect("CK_CODE");
		_bstr_t bstrCode = (_bstr_t)pRecordset->GetCollect("CK_SCODE");
		_bstr_t bstrName = (_bstr_t)(pRecordset->GetCollect("CK_NAME"));
		_bstr_t bstrDesc = (_bstr_t)(pRecordset->GetCollect("CK_DESCRIPTION"));
		z_uint32 nModelCode = (z_uint32)(pRecordset->GetCollect("CK_MODEL_CODE"));

		CheckCase* pCase = new CheckCase(nID);
		pCase->SetCode(nCode);
		pCase->SetCode(bstrCode);
		pCase->SetName(bstrName);
		pCase->SetModeCode(nModelCode);
		pCase->SetDescription(bstrDesc);
		return pCase;
	}

	//////////////////////////////////////////////////////////////////////////

	CheckCaseManager* CheckCaseManager::m_pInstance = NULL;

	CheckCaseManager* CheckCaseManager::GetInstance()
	{
		return m_pInstance;

	}

	CheckCaseManager::CheckCaseManager()
	{
	}


	CheckCaseManager::~CheckCaseManager()
	{
	}

	void CheckCaseManager::SetConnection(Connection* pConnection)
	{
		m_pConnection = pConnection;
	}

	RESULTCODE CheckCaseManager::Load()
	{
		return RC_SUCCESS;
	}

	RESULTCODE CheckCaseManager::Initialize()
	{
		return RC_SUCCESS;
	}

	RESULTCODE CheckCaseManager::CreateCase(const char* szName, z_uint32 code, const char* szCode, z_uint32 modelCode, const char* szDescription)
	{
		char szSQL[Z_SQL_MAX];
		memset(szSQL, 0, Z_SQL_MAX);
		BuildInsertModelSQL(szSQL, Z_SQL_MAX, code, szCode, szName, modelCode, szDescription);

		TRACE1("%s\n", szSQL);
		return m_pConnection->Execute(szSQL);
	}

	EnumCheckCase* CheckCaseManager::GetCases()
	{
		EnumCheckCase* pCases = new EnumCheckCase();

		char szSQL[Z_SQL_MAX];
		memset(szSQL, 0, Z_SQL_MAX);
		BuildGetCaseSQL(szSQL, Z_SQL_MAX);

		ck::Command* pCommand = m_pConnection->CreateCommand();
		ADODB::_RecordsetPtr pRecordset = pCommand->ExecuteQuery(szSQL);
		if (pRecordset != NULL)
		{
			_variant_t var;
			while (!pRecordset->adoEOF)
			{
				CheckCase* pCase = CreateCaseObject(pRecordset);
				if (pCase)
				{
					pCases->Add(pCase);
				}

				pRecordset->MoveNext();
			}
		}
		pCommand->Release();
		pCommand = NULL;

		return pCases;
	}

	EnumCheckCase* CheckCaseManager::GetCases(z_uint32 nModelCode)
	{
		EnumCheckCase* pCases = new EnumCheckCase();

		char szSQL[Z_SQL_MAX];
		memset(szSQL, 0, Z_SQL_MAX);
		BuildGetCasesSQL(szSQL, Z_SQL_MAX, nModelCode);

		ck::Command* pCommand = m_pConnection->CreateCommand();
		ADODB::_RecordsetPtr pRecordset = pCommand->ExecuteQuery(szSQL);
		if (pRecordset != NULL)
		{
			_variant_t var;
			while (!pRecordset->adoEOF)
			{
				CheckCase* pCase = CreateCaseObject(pRecordset);
				if (pCase)
				{
					pCases->Add(pCase);
				}

				pRecordset->MoveNext();
			}
		}
		pCommand->Release();
		pCommand = NULL;

		return pCases;
	}


	CheckCase* CheckCaseManager::GetCase(const char* szName)
	{
		CheckCase* pCase = NULL;

		char szSQL[Z_SQL_MAX];
		memset(szSQL, 0, Z_SQL_MAX);
		BuildGetCaseSQL(szSQL, Z_SQL_MAX, szName);

		ck::Command* pCommand = m_pConnection->CreateCommand();
		ADODB::_RecordsetPtr pRecordset = pCommand->ExecuteQuery(szSQL);
		if (pRecordset != NULL)
		{
			_variant_t var;
			if (!pRecordset->adoEOF)
			{
				pCase = CreateCaseObject(pRecordset);
			}
		}
		pCommand->Release();
		pCommand = NULL;

		return pCase;
	}

	CheckCase* CheckCaseManager::GetCase(z_uint32 nCode)
	{
		CheckCase* pCase = NULL;

		char szSQL[Z_SQL_MAX];
		memset(szSQL, 0, Z_SQL_MAX);
		BuildGetCaseSQL(szSQL, Z_SQL_MAX, nCode);

		ck::Command* pCommand = m_pConnection->CreateCommand();
		ADODB::_RecordsetPtr pRecordset = pCommand->ExecuteQuery(szSQL);
		if (pRecordset != NULL)
		{
			_variant_t var;
			if (!pRecordset->adoEOF)
			{
				pCase = CreateCaseObject(pRecordset);
			}
		}
		pCommand->Release();
		pCommand = NULL;

		return pCase;
	}

	RESULTCODE CheckCaseManager::AddRule(z_uint32 nCaseCode, z_uint32 nItemCode, z_uint32 nOperCode, z_uint32 nModCode)
	{
		char szSQL[Z_SQL_MAX];
		memset(szSQL, 0, Z_SQL_MAX);
		BuildCaseAddRuleSQL(szSQL, Z_SQL_MAX, nCaseCode, nItemCode, nOperCode, nModCode);
		
		return m_pConnection->Execute(szSQL);
	}

	RESULTCODE CheckCaseManager::DeleteCase(z_uint32 nCaseCode)
	{
		RESULTCODE rc = RC_SUCCESS;
		rc = RemoveRules(nCaseCode);
		if (rc != RC_SUCCESS)
		{
			return rc;
		}

		rc = RemoveCase(nCaseCode);
		if (rc != RC_SUCCESS)
		{
			return rc;
		}

		return RC_SUCCESS;
	}

	RESULTCODE CheckCaseManager::RemoveCase(z_uint32 nCaseCode)
	{
		char szSQL[Z_SQL_MAX];
		memset(szSQL, 0, Z_SQL_MAX);
		BuildRemoveCaseSQL(szSQL, Z_SQL_MAX, nCaseCode);
		TRACE0(szSQL);

		return m_pConnection->Execute(szSQL);
	}

	RESULTCODE CheckCaseManager::RemoveRules(z_uint32 nCaseCode)
	{
		char szSQL[Z_SQL_MAX];
		memset(szSQL, 0, Z_SQL_MAX);
		BuildRemoveRulesSQL(szSQL, Z_SQL_MAX, nCaseCode);
		TRACE0(szSQL);

		return m_pConnection->Execute(szSQL);
	}

	bool CheckCaseManager::FindCheckItem(z_uint32 nCaseCode, z_uint32 nItemCode)
	{
		char szSQL[Z_SQL_MAX];
		memset(szSQL, 0, Z_SQL_MAX);
		BuildFindCheckItemSQL(szSQL, Z_SQL_MAX, nCaseCode, nItemCode);
		TRACE0(szSQL);

		int nCount = 0;
		ck::Command* pCommand = m_pConnection->CreateCommand();
		ADODB::_RecordsetPtr pRecordset = pCommand->ExecuteQuery(szSQL);
		if (pRecordset != NULL)
		{
			_variant_t var;
			if (!pRecordset->adoEOF)
			{
				nCount = pRecordset->GetCollect("CNT");
			}
		}
		pCommand->Release();
		pCommand = NULL;

		return (nCount>0);
	}

	EnumOperator* CheckCaseManager::GetOperators(z_uint32 nCaseCode)
	{
		EnumOperator* pOpers = new EnumOperator();

		char szSQL[_MAX_PATH];
		BuildGetCaseOperatorsSQL(szSQL, _MAX_PATH, nCaseCode);
		TRACE0(szSQL);

		ck::Command* pCommand = m_pConnection->CreateCommand();
		ADODB::_RecordsetPtr pRecordset = pCommand->ExecuteQuery(szSQL);
		if (pRecordset != NULL)
		{
			int nOperCode = 0;
			int nModCode = 0;
			ck::Operator* pOperator = NULL;
			ck::Module* pModule  = NULL;
			ck::ModuleManager* pModManager = ck::ModuleManager::GetInstance();

			_variant_t var;
			while (!pRecordset->adoEOF)
			{
				nOperCode = pRecordset->GetCollect("CK_OPER_CODE");
				nModCode = pRecordset->GetCollect("CK_MODULE_CODE");

				if (nOperCode >= 0 && nModCode >= 0)
				{
					pModule = pModManager->GetModuleByCode(nModCode);
					if (pModule)
					{
						pOperator = pModule->GetOperatorByCode(nOperCode);
						if (pOperator)
						{
							pOpers->Add(pOperator);
						}
					}
				}
				pRecordset->MoveNext();
			}
		}
		pCommand->Release();
		pCommand = NULL;

		return pOpers;
	}

}

